package xyf.p10.dp;

/**
 * ClassName: P0309_MaxProfit
 * Description:
 * Author: xyf
 * Date: 9/9/22 10:28 AM
 * Version: 1.0
 **/
public class P0309_MaxProfit
{
    public int maxProfit(int[] prices)
    {
        int[][] dp = new int[prices.length][3];

        //不持有股票，不在冷冻期
        dp[0][0] = 0;

        //不持有股票，在冷冻期
        dp[0][1] = 0;

        //持有股票
        dp[0][2] = -prices[0];

        for (int i = 1; i < prices.length; i++)
        {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);
            dp[i][1] = dp[i - 1][2] + prices[i];
            dp[i][2] = Math.max(dp[i - 1][0] - prices[i], dp[i - 1][2]);
        }

        return Math.max(Math.max(dp[prices.length - 1][0], dp[prices.length - 1][1]), dp[prices.length - 1][2]);
    }
}